<template>
  <el-dialog title="编辑菜单" :close-on-click-modal="false" @close="close" :visible.sync="visible">
    <el-form :model="form" :rules="rules" ref="form">
      <el-form-item label="排序号" :label-width="formLabelWidth" prop="sortNo">
        <el-input v-model="form.sortNo"/>
      </el-form-item>
      <el-form-item label="菜单名" :label-width="formLabelWidth" prop="name">
        <el-input v-model="form.name"/>
      </el-form-item>
      <el-form-item label="CODE" :label-width="formLabelWidth" prop="code">
        <el-input v-model="form.code"/>
      </el-form-item>
      <el-form-item label="URL" :label-width="formLabelWidth" prop="url">
        <el-input v-model="form.url"/>
      </el-form-item>
      <el-form-item label="父菜单" :label-width="formLabelWidth" prop="parentId">
        <el-select v-model="form.parentId" filterable placeholder="请选择">
          <template v-for="item in resourceOptions">
            <el-option
              :key="item.key"
              :label="item.value"
              :value="item.key">
            </el-option>
          </template>
        </el-select>
      </el-form-item>
      <el-form-item label="功能类型" :label-width="formLabelWidth" prop="type">
        <el-select v-model="form.type" filterable placeholder="请选择">
          <template v-for="item in typeOptions">
            <el-option
              :key="item.key"
              :label="item.value"
              :value="item.key">
            </el-option>
          </template>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <c-button type="primary" @click="btnOk">确 定</c-button>
    </div>
  </el-dialog>
</template>

<script>
import Options from '@/constants/Options'

export default {
  name: 'SDialogEdit',
  props: {
    data: {
      type: Object,
      default () {
        return {
          id: '',
          sortNo: '',
          name: '',
          code: '',
          url: '',
          parentId: '',
          type: ''
        }
      }
    }
  },
  data () {
    return {
      formLabelWidth: '120px',
      visible: true,
      resourceOptions: [],
      form: {
        id: this.data.id,
        sortNo: this.data.sortNo,
        name: this.data.name,
        code: this.data.code,
        url: this.data.url,
        parentId: this.data.parentId,
        type: this.data.type
      },
      typeOptions: Options.ResTypeOptions,
      rules: {
        name: [
          ...this.$checkRules.resource.resName
        ],
        code: [
          ...this.$checkRules.resource.code
        ],
        url: [
          ...this.$checkRules.resource.url
        ],
        parentId: [
          ...this.$checkRules.resource.parentId
        ],
        type: [
          ...this.$checkRules.resource.type
        ]
      }
    }
  },
  created () {
    this.$service.system.queryResourceList().then(response => {
      this.resourceOptions = response.data
    })
  },
  methods: {
    close () {
      this.$emit('close')
    },
    btnOk (btn) {
      this.$refs['form'].validate((valid) => {
        // 校验通过
        if (valid) {
          this.$service.system.updateResource(this.form).then(() => {
            this.$emit('ok')
            this.visible = false
          }).finally(() => {
            btn.isLoading(false)
          })
        } else {
          btn.isLoading(false)
        }
      })
    }
  }
}
</script>

<style>

</style>
